﻿using System.Collections.Generic;
using System.Linq;
using Microsoft.Practices.Unity;
using RG.Common.Provider;
using RG.Consts;
using RealtorGuide.Classes;

namespace RG.Classes.Impl.Beta
{
    public class ManagerFakeService : IManagerService
    {
        public Manager Get(int id)
        {
            return ClassesContainer.Managers.FirstOrDefault(x => x.Id == id);
        }


        public Manager Put(Manager item)
        {
            if (item.Id != 0)
            {
                ClassesContainer.Managers.Remove(ClassesContainer.Managers.FirstOrDefault(x => x.Id == item.Id));
                ClassesContainer.Managers.Add(item);
                return item;
            }

            item.Id = ++ClassesContainer.ManagerGenerator;
            ClassesContainer.Managers.Add(item);
            return item;
        }


        public List<Manager> List(ManagersQueryParams param)
        {
            return ReturnFiltered(ClassesContainer.Managers, param);
        }


        public List<int> ListPrepare(ManagersQueryParams param)
        {
            return ReturnFiltered(ClassesContainer.Managers, param).Select(x => x.Id).ToList();
        }


        private List<Manager> ReturnFiltered(List<Manager> source, ManagersQueryParams param)
        {
            var toReturn = source;

            if (param.Ids != null && param.Ids.Any())
                toReturn = toReturn.Where(x => param.Ids.Contains(x.Id)).ToList();
           
            return toReturn;
        }

    }
}
